package com.zmn.oms.services.impl.conf.workturn.servcategory;

import com.zmn.base.product.common.dto.categ.CategProductEcErpDRO;
import com.zmn.base.product.common.query.categ.CategProductEcErpQuery;
import com.zmn.base.product.dubbo.interfaces.categ.CategProductEcErpListRemoteService;
import com.zmn.base.product.dubbo.interfaces.categ.CategProductListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.oms.model.entity.conf.workturn.servcategory.PlatServCategory;
import com.zmn.oms.persistence.interfaces.conf.workturn.servcategory.PlatServCategoryDAO;
import com.zmn.oms.services.interfaces.conf.workturn.servcategory.PlatServCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台开通服务分类
 *
 * @author linfeng
 * @since 2020/04/13 12:19:07
 */
@Service
public class PlatServCategoryServiceImpl implements PlatServCategoryService {

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private CategProductEcErpListRemoteService categProductEcErpListRemoteService;
    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private CategProductListRemoteService categProductListRemoteService;

    @Resource
    private PlatServCategoryDAO platServCategoryDAO;

    @Override
    public PlatServCategory findByKey(Integer id) {
        return platServCategoryDAO.findByKey(id);
    }

    @Override
    public List<PlatServCategory> listByPlat(Integer plat) {
        return platServCategoryDAO.listByPlat(plat);
    }

//    @Override
//    public List<PlatServCategoryVO> listVoByPlat(Integer plat) {
//        List<PlatServCategoryVO> servCategoryVOList = platServCategoryDAO.listVoByPlat(plat);
//        List<Integer> categIds = servCategoryVOList.stream().map(PlatServCategoryVO::getCategId).collect(Collectors.toList());
//        CategProductQuery query = new CategProductQuery();
//        query.setCategIds(categIds);
//        ResponseDTO<List<CategProduct>> listResponseDTO = categProductListRemoteService.listByQuery(query);
//        List<CategProduct> categList = listResponseDTO.getData();
//    }

    @Override
    public List<PlatServCategory> listByPlatAndServCategId(Integer plat, Integer servCategId) {
        return platServCategoryDAO.listByPlatAndServCategId(plat, servCategId);
    }

    @Override
    public Integer countOpenCategByPlatAndCateg(Integer plat, Integer servCategId, Integer showCategTwoId) {
        // 前台分类 转化为后台分类
        CategProductEcErpQuery query = new CategProductEcErpQuery();
        query.setEcCategId(showCategTwoId);
        ResponseDTO<List<CategProductEcErpDRO>> listResponseDTO = categProductEcErpListRemoteService.listByQuery(query);
        List<CategProductEcErpDRO> productEcErpVOList = listResponseDTO.getData();
        if (CollectionUtils.isEmpty(productEcErpVOList)) {
            return 0;
        }
        List<Integer> categTwoIdList = productEcErpVOList.stream().map(CategProductEcErpDRO::getErpCategId).collect(Collectors.toList());
        return platServCategoryDAO.countOpenCategByPlatAndCateg(plat, servCategId, categTwoIdList);
    }

    @Override
    public int updateByKey(PlatServCategory record) {
        return platServCategoryDAO.updateByKey(record);
    }

    @Override
    public int insert(PlatServCategory record) {
        return platServCategoryDAO.insert(record);
    }

    @Override
    public int insertBatch(List<PlatServCategory> list) {
        return platServCategoryDAO.insertBatch(list);
    }

    @Override
    public int deleteByKey(Integer id) {
        return platServCategoryDAO.deleteByKey(id);
    }

    @Override
    public int deleteByPlat(Integer plat) {
        return platServCategoryDAO.deleteByPlat(plat);
    }

    @Override
    public int deleteByPlatAndServCategId(Integer plat, Integer servCategId) {
        return platServCategoryDAO.deleteByPlatAndServCategId(plat, servCategId);
    }

    @Override
    public int deleteBatch(List<PlatServCategory> list) {
        return platServCategoryDAO.deleteBatch(list);
    }
}
